<?php

  /*
  
      TravianParser v1.3.0.0
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/
      Contact: uroskn@gmail.com

      Requires:
        - nHTTP client 1.0 (http://n00bz.biz/upload/http.php.html)

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

      --------------------------------------------------------------------------

      To-Do:
        - Fix remaining bugs (mostly login and session timeout problem)
        - Shorten/Optimize code
        - Redefine constants (Add TPARSER_ prefix)
        - redefine functions (Change prefix from travian_ to tparser_)
        - Redefine HTTP calls by adding prefix TPARSER_HTTP_ (http.php)
        - Document code

      --------------------------------------------------------------------------

      Changed in version 1.1.1:
        - Made parser totally language independent.

      Changed in version 1.1.2:
        - Fixed errors in reports (no player & village for baackup reporst)
        - Fixed bug at constructing new building (incorrect paramaters)

      Added in version 1.1.3:
        - Multi-handle functioning (i know, i know)...
        
      Changed in version 1.1.4:
        - Added troop type detection
        - Fixed bugs in Barracks (travian_return_train_info)
        - Fixed bugs in rally point (travian_recall_troops)

      Changed in version 1.1.5:
        - Fixed marketplace problems
        - Fixed research probles
        - Research now return also a troop ID.
        
      Added in version 1.2.0:
        - <span> appearing at "not enough resources for construction" error fixed
        - Added function to extract production from anywhere (travian_extract_production)
        
      Added in version 1.3.0:
        - Almost all functions requireing field ID, it's now possible to specify GID.
        
      Fixed in versioin 1.3.1:
        - Troop training was fucking again. Stupid travian admins :@

  */

  define("TPARSER_PRIMARY", 1);
  define("TPARSER_SEC",     3);
  define("TPARSER_REM",     1);
  define("TPARSER_VERSION", TPARSER_PRIMARY.".".TPARSER_SEC.".".TPARSER_REM);
  define("TPARSER_DATE",    1221495750);

  include("http.php");
  if (!defined("NHTTP_OK")) die("TravianParser (".TPARSER_VERSION.") error: Unable to load additonal files (http client)");

  /*
     ******************************************************************************
          E X T E R N A L   C O N S T A N T S
     ******************************************************************************
  */

  define("BUILDING_FREE",       0);
  define("BUILDING_BAKERY",     9);
  define("BUILDING_WAREHOUSE",  10);
  define("BUILDING_GRANARAY",   11);
  define("BUILDING_BLACKSMITH", 12);
  define("BUILDING_ARMOURY",    13);
  define("BUILDING_TSQUARE",    14);
  define("BUILDING_MAINBUILD",  15);
  define("BUILDING_RALLYPOINT", 16);
  define("BUILDING_MARKET",     17);
  define("BUILDING_EMBASSY",    18);
  define("BUILDING_BARRACS",    19);
  define("BUILDING_STABLE",     20);
  define("BUILDING_WORKSHOP",   21);
  define("BUILDING_ACADEMY",    22);
  define("BUILDING_CRANNY",     23);
  define("BUILDING_TOWNHALL",   24);
  define("BUILDING_RESIDENCE",  25);
  define("BUILDING_PALACE",     26);
  define("BUILDING_TREASURY",   27);
  define("BUILDING_TRADEOFFICE",28);
  define("BUILDING_GBARRACS",   29);
  define("BUILDING_GSTABLE",    30);
  define("BUILDING_CITYWALL",   31);
  define("BUILDING_EARTHWALL",  32);
  define("BUILDING_PALISADE",   33);
  define("BUILDING_STONEMASON", 34);
  define("BUILDING_BREWERY",    35);
  define("BUILDING_TRAPPER",    36);
  define("BUILDING_HEROMAINS",  37);
  define("BUILDING_GWAREHOUSE", 38);
  define("BUILDING_GGRANARY",   39);
  define("BUILDING_WONDER",     40);

  define("SEND_TROOPS_DEFENSE", 2);
  define("SEND_TROOPS_ATTACK",  3);
  define("SEND_TROOPS_RAID",    4);

  define("CATAPULT_ANY",        99);
  define("CATAPULT_WOOD",       1);
  define("CATAPULT_CLAY",       2);
  define("CATAPULT_IRON",       3);
  define("CATAPULT_CROP",       4);
  define("CATAPULT_WOODBUILD",  5);
  define("CATAPULT_BRICKYARD",  6);
  define("CATAPULT_IRONBUILD",  7);
  define("CATAPULT_MILL",       8);
  define("CATAPULT_BAKERY",     9);
  define("CATAPULT_WAREHOUSE",  10);
  define("CATAPULT_GRANARY",    11);
  define("CATAPULT_WEAPONRY",   12);
  define("CATAPULT_AURMORY",    13);
  define("CATAPULT_TRAINING",   14);
  define("CATAPULT_MAIN_BUILD", 15);
  define("CATAPULT_RALLYPOINT", 16);
  define("CATAPULT_MARKETPLACE",17);
  define("CATAPULT_EMBASSY",    18);
  define("CATAPULT_BARRACS",    19);
  define("CATAPULT_STABLE",     20);
  define("CATAPULT_WORKSHOP",   21);
  define("CATAPULT_ACADEMY",    22);
  define("CATAPULT_TOWN_NHALL", 24);
  define("CATAPULT_RESIDENCE",  25);
  define("CATAPULT_PALACE",     26);
  define("CATAPULT_TREASURY",   27);
  define("CATAPULT_TRADEOFFICE",28);
  define("CATAPULT_BIGBARRACS", 29);
  define("CATAPULT_BIGSTABLE",  30);
  define("CATAPULT_HERORBUILD", 37);

  define("SPY_RESOURCES_UNITS", 1);
  define("SPY_DEFENSE_UNITS",   2);

  define("TROOPS_ROMANS",       1);
  define("TROOPS_GAULS",        2);
  define("TROOPS_TEUTONS",      3);
  define("TROOPS_NATURE",       4);
  define("TROOPS_NATARS",       5);

  /*
     ******************************************************************************
          I N T E R N A L   C O N S T A N T S
     ******************************************************************************
  */

  define("VILLAGE_NAME",        '<h1>');
  define("VILLAGE_NAME_END",    '</h1></div>');
  define("WOOD_START",          '<td id=l4 title=');
  define("CLAY_START",          '<td id=l3 title=');
  define("IRON_START",          '<td id=l2 title=');
  define("CROP_START",          '<td id=l1 title=');
  define("CROP_PROD_START",     '<td class="s7"> &nbsp;<img class="res"');
  define("CROP_PROD_START2",    '<td style="padding-top:4px;"');
  define("RES_PROD_START",      '</td><td align="right"><b>');

  define("LOGIN_USER",          'class="fm fm110" type="text" name="');
  define("LOGIN_PASS",          'type="password" name="');
  define("LOGIN_HIDD",          '<p align="center"><input type="hidden" name="');

  define("CONST_START",         '<tr><td><a href="?d=');
  define("CONST_X_END",         '"></a></td><td>');
  define("CONST_TIMER",         '</td><td><span id=timer');
  define("CONST_SPAN",          '</span><span>');
  define("CONST_SPANT",         5);

  define("BUILD_NAME_START",    '<h1><b>');
  define("BUILD_NAME_START2",   '<h2>');
  define("BUILD_NAME_FALSE",    '</table><br><h2>');

  define("RES_DEF_BUILD_START", '<br /><br /><img class="res" src="img/un/r/1.gif">');
  define("RES_DEF_B_START2",    '<table class="f10"><tr><td><img class="res" src="img/un/r/1.gif">');
  define("BUILD_START_ERROR",   '<span class="c">');
  define("BUILD_START_ERROR2",  '<p class="c">');
  define("BUILD_A",             '.php?a=');
  define("AREA",                '<area href="');

  define("RESOURCE_WOOD",       'img/un/r/1.gif');
  define("RESOURCE_CLAY",       'img/un/r/2.gif');
  define("RESOURCE_IRON",       'img/un/r/3.gif');
  define("RESOURCE_CROP",       'img/un/r/4.gif');

  define("TRAIN_TROOP_TYPE_S",  '<td width="6%" rowspan="2" valign="top" class="s7"><img class="unit" src="img/un/u/');
  define("TRAIN_Z_START",       '<input type="hidden" name="z" value="');
  define("TRAIN_TNAME_START",   '<td class="s7"><div><a href="#" onClick="return Popup(');
  define("TRAIN_TROOPS_COUNT",  ' <span class="c f75">(');
  define("TRAIN_TROOPS_RES",    '<img src="img/un/a/x.gif" width="1" height="15"><img class="res" src="img/un/r/1.gif">');
  define("TRAIN_TROOPS_CTRAIN", '; return false;">(');
  define("TRAIN_IS_STH",        '<td width="5%"><img class="unit" src="img/un/u/');
  define("TRAIN_COUNT",         '<td width="6%" align="right">');
  define("TRAIN_NAME",          '<td width="39%" class="s7">');
  define("TRAIN_TIME",          '<td width="25%"><span id=timer');
  define("TRAIN_NEXT_FIN",      '<span id=timer');

  define("TRADE_BID_START",     '</tr><tr bgcolor="#FFFFFF" align="center"><td><a href="');
  define("TRADE_OFFER_RES",     '<td><img class="res" src="');
  define("TRADE_OFFER_PLAYER",  '<a href="karte.php?d=');
  define("TRADE_MERCHANTS",     'var haendler = ');
  define("TRADE_CARRY",         'var carry = ');
  define("TRADE_VILLAGESEND",   '<a href="karte.php?d=');
  define("TRADE_TRANSPORT",     '<table cellspacing="1" cellpadding="2" class="tbg">');
  define("TRADE_TRANSPORT_END", '</td></tr></table></p><p');
  define("TRADE_PLAYER",        '<td width="21%"><a href="spieler.php?uid=');
  define("TRADE_LOCATION",      '"><span class="c0">');
  define("TRADE_CARD",          '<a href="karte.php?d=');
  define("TRADE_TIMER",         '</td><td><span id=timer');
  define("TRADE_RESOURCES",     '<td class="s7" colspan="2"><span class="');
  define("TRADE_RETURN",        'c f10');
  define("TRADE_SEND_ERROR",    '</script><p class="b c5">');
  define("TRADE_INPUT_HIDDEN",  '<input type="hidden" name="a" value="');
  define("TRADE_INPUT_HIDDEN2", '<input type="hidden" name="sz" value="');
  define("TRADE_INPUT_HIDDEN3", '<input type="hidden" name="kid" value="');
  define("TRADE_VILLAGE_NAME",  '<p class="f135">');
  define("TRADE_PLAYER_NAME",   '</td><td><a href="spieler.php?uid=');
  define("TRADE_TTIME",         '<tr class="left"><td>');
  define("TRADE_FINISH_LIST",   '#h2">&laquo;</a><span class="c"><b>&raquo;</b>');
  define("TRADE_MERCHANT_BID",  '</table><p class="f10">');

  define("REPORT_START",        '<td width="22"><input type="Checkbox" name="');
  define("REPORT_NONE",         '</td></tr><tr><td colspan="4" class="c">');
  define("REPORT_URL_LONG",     '<td class="s7"><a href="berichte.php?id=');
  define("REPORT_URL",          'berichte.php?id=');
  define("REPORT_DATE",         '<td width="125" nowrap>');
  define("REPORT_TRADE_RESS",   '<td class="s7"><img class="res" src="');
  define("REPORT_TROOP_EXISTS", 'img/un/u/');
  define("REPORT_PLAYER_NAME",  '<a href="spieler.php?uid=');
  define("REPORT_VILLAGE_NAME", '<a href="karte.php?');
  define("REPORT_ATTACKER",     '<td width="21%" class="c2 b">');
  define("REPORT_TROOP_START",  '</td></tr><tr><td>');
  define("REPORT_DEFINE_TEUT",  'img/un/u/11.gif" title="');
  define("REPORT_DEFINE_GAUL",  'img/un/u/24.gif" title="');
  define("REPORT_DEFINE_ROMA",  'img/un/u/1.gif" title="');
  define("REPORT_DEFINE_NATUR", 'img/un/u/31.gif" title="');
  define("REPORT_DEFINE_NATAR", 'img/un/u/41.gif" title="');
  define("REPORT_CROP_USE",     '<img class="res" src="');
  define("REPORT_RESOURCE_S",   '</td><td class="s7" colspan="10">');
  define("REPORT_END",          '</table></p></div></div></div>');
  define("REPORT_SEPARATOR",    '<tr class="cbg1">');
  define("REPORT_SEGMENT_END",  '</td></tr></table>');
  define("REPORT_ADD_INFO",     '<td colspan="1');
  define("REPORT_TITLE_ATTACK", '<td colspan="10">');
  define("REPORT_VILLAGE_NAME", '<a href="karte.php?d=');

  define("TROOP_UPGRADE_START", '<td class="s7"><div><a href="#" onClick="return Popup(');
  define("TROOP_UPGRADE_LEVEL", '</a> <span class="f8">(');
  define("TROOP_UPGRADE_RES",   '<img src="img/un/a/x.gif" width="1" height="15"><img class="res" src="img/un/r/1.gif">');
  define("TROOP_UPGRADE_LINK",  '%"><a href="build.php?id=');
  define("TROOP_UPGRADE_ERROR", '%"><div class="c">');
  define("TROOP_UPGRADE_NAME",  '<td width="44%" class="s7">');
  define("TROOP_UPGRADE_TIME",  '<td width="25%"><span id=timer1>');
  define("TROOP_UPGRADE_AT",    '<td width="25%">');
  define("TROOP_UPGRATE_SEP",   '<table width="100%" class="f10" cellspacing="2" cellpadding="0">');

  define("MAIN_SELECT_REM",     '<select name="abriss" class="f8">');
  define("MAIN_REM_ACTIVE",     '"><img src="img/un/a/del.gif" width="12" height="12" border="0" title="');
  define("MAIN_REM_LINK",       '</p><p><table width="100%" class="f10"><tr><td><a href="build.php?gid=');
  define("MAIN_TITLE_TIMER",    '</td><td><span id=timer1>');
  define("MAIN_GID_HIDDEN",     '<input type="hidden" name="gid" value="');
  define("MAIN_OPTION",         '<option value="');

  define("SENDT_ERROR",         '<div class="f10 e b">');
  define("SENDT_ID",            '<input type="hidden" name="id" value="');
  define("SENDT_TD_TIME",       '<td width="50%">');
  define("SENDT_HIDDEN",        '<input type="hidden" name="');
  define("SENDT_HIDDEN_V",      'value="');
  define("SENDT_VILLAGE",       '<td class="s7"><a href="karte.php?d=');
  define("SENDT_PLAYER",        '<td class="s7"><a href="spieler.php?uid=');
  define("SENDT_TROOPS_START",  '<a href="#" onClick="document.snd.t');
  define("SENDT_TROOPS_END",    '.value=');

  define("S7_CLASS_TD",         '<td class="s7">');

  define("RPOINT_BOX_START",    '<table cellspacing="1" cellpadding="2" class="tbg">');
  define("RPOINT_BOX_END",      '</table>');
  define("RPOINT_MAP_POS",      '<td width="21%"><a href="karte.php?d=');
  define("RPOINT_VILL_NAME1",   '<span class="c0">');
  define("RPOINT_TITLE_START",  '<td colspan="10"');
  define("RPOINT_TITLE_MAP",    '<a href="karte.php?d=');
  define("RPOINT_UNITS_START",  '<tr class="unit">');
  define("RPOINT_DEFINE_TEUT",  REPORT_DEFINE_TEUT);
  define("RPOINT_DEFINE_GAUL",  REPORT_DEFINE_GAUL);
  define("RPOINT_DEFINE_ROMA",  REPORT_DEFINE_ROMA);
  define("RPOINT_DEFINE_NATAR", REPORT_DEFINE_NATAR);
  define("RPOINT_NUM_T_START",  '</td></tr><tr><td>');
  define("RPOINT_TIMER_sTART",  '<span id=timer');
  define("RPOINT_TIMER_AT",     '<td width="50%">');
  define("RPOINT_CROP_USAGE",   '<table width="100%" cellspacing="0" cellpadding="0" class="f10"><tr><td width="50%">');
  define("RPOINT_CROP_USAGE2",  '</td><td class="s7" colspan="11">');
  define("RPOINT_CROP_USAGE3",  '</td><td class="s7" colspan="10">');
  define("RPOINT_CALLBACK_URL", '<td width="50%" class="right"><a href="a2b.php?d=');
  define("RPOINT_CANCEL",       '</td><td class="r7"><a href="build.php?');
  define("RPOINT_RETURN_A",     '<input type="hidden" name="a" value="');
  define("RPOINT_RETURN_SEP",   '<table width="100%" cellspacing="0" cellpadding="0" class="f10">');
  define("RPOINT_SHOOT_HOSTAGE",'<td width="50%" class="right"><a href="build.php?id=');

  /*
     ******************************************************************************
         I N T E R N A L   F U N C T I O N S
     ******************************************************************************
  */

  function _travian_restore_cookie($handle)
  {
    http_restore_cookies(NULL);
    http_restore_cookies($handle["cookie"]);
  }

  function _travian_parse_needed($handle, $parse, $file)
  {
    if (!$parse)
    {
      _travian_restore_cookie($handle);
      $text = http_get("http://".$handle["serv"]."/$file");
      $text = $text["body"];
    }
    else $text = $handle;
    return $text;
  }

  function _travian_return_resource_id($text)
  {
    if ($text == RESOURCE_WOOD) return 1;
    if ($text == RESOURCE_CLAY) return 2;
    if ($text == RESOURCE_IRON) return 3;
    if ($text == RESOURCE_CROP) return 4;
  }
  
  /* ****************************************************************************** */

  function travian_recall_troops($handle, $d_param, $c_param, $troops, $retinfo = False)
  {
    _travian_restore_cookie($handle);
    $text = http_get("http://".$handle["serv"]."/a2b.php?d=$d_param&c=$c_param");
    $text = $text["body"];
    $var["gid"] = 16;
    $temp = substr($text, strpos($text, RPOINT_RETURN_A) + strlen(RPOINT_RETURN_A));
    $var["a"] = substr($temp, 0, strpos($temp, '"'));
    $var["d"] = $d_param;
    $text = substr($text, 0, strpos($text, RPOINT_RETURN_SEP));
    if (strpos($text, RPOINT_DEFINE_TEUT) !== False) $var["type"] = 3;
    elseif (strpos($text, RPOINT_DEFINE_GAUL) !== False) $var["type"] = 2;
    else $var["type"] = 1;
    for ($i = 1; $i <= 11; $i++)
    {
      $rvalue = '<input class="fm" type="text" name="t['.$i.']" value="';
      if (strpos($text, $rvalue))
      {
        $temp = substr($text, strpos($text, $rvalue) + strlen($rvalue));
        $var["troops"][$i - 1] = substr($temp, 0, strpos($temp, '"'));
      }
      else $var["troops"][$i - 1] = 0;
    }
    if (!$retinfo)
    {
      $data = array(
        "gid"  => "16",
        "a"    => $var["a"],
        "d"    => $d_param
      );
      for ($i = 0; $i <= 10; $i++) $data["t[".($i + 1)."]"] = $troops[$i];
      http_post("http://".$handle["serv"]."/build.php", $data);
      return;
    }
    else return $var;
  }

  function travian_return_rally_point_info($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?id=39");
    $i = -1;
    while (True)
    {
      $i++;
      $text = substr($text, strpos($text, RPOINT_BOX_START) + strlen(RPOINT_BOX_START));
      $data = substr($text, 0, strpos($text, RPOINT_BOX_END));
      $data = substr($data, strpos($data, RPOINT_MAP_POS) + strlen(RPOINT_MAP_POS));
      $var[$i]["orign"]["link"] = "d=".substr($data, 0, strpos($data, '"'));
      $data = substr($data, strpos($data, RPOINT_VILL_NAME1) + strlen(RPOINT_VILL_NAME1));
      $var[$i]["orign"]["name"] = substr($data, 0, strpos($data, '</span>'));
      $data = substr($data, strpos($data, RPOINT_TITLE_START) + strlen(RPOINT_TITLE_START));
      $temp = substr($data, 0, strpos($data, '</td>'));
      $temp = substr($temp, strpos($temp, '>') + 1);
      if (strpos($temp, RPOINT_TITLE_MAP) !== False)
      {
        $temp = substr($temp, strpos($temp, RPOINT_TITLE_MAP) + strlen(RPOINT_TITLE_MAP));
        $var[$i]["title"]["url"] = "d=".substr($temp, 0, strpos($temp, '"'));
        $temp = substr($temp, strpos($temp, '"') + 2);
      }
      $var[$i]["title"]["title"] = strip_tags(str_replace("\n", '', $temp));
      $data = substr($data, strpos($data, RPOINT_UNITS_START) + strlen(RPOINT_UNITS_START));
      if (strpos($data, RPOINT_DEFINE_TEUT) !== False) $var[$i]["type"] = 3;
      elseif (strpos($data, RPOINT_DEFINE_GAUL) !== False) $var[$i]["type"] = 2;
      elseif (strpos($data, RPOINT_DEFINE_NATAR) !== False) $var[$i]["type"] = 5;
      else $var[$i]["type"] = 1;
      $data = substr($data, strpos($data, RPOINT_NUM_T_START) + strlen(RPOINT_NUM_T_START));
      $data = substr($data, strpos($data, '</td>') + 5);
      $temp = substr($data, 0, strpos($data, '</tr>'));
      $var[$i]["troops"] = explode('|', strip_tags(str_replace('</td>', '|', $temp)));
      if (strpos($data, RPOINT_TIMER_sTART) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_TIMER_sTART) + strlen(RPOINT_TIMER_sTART));
        $data = substr($data, strpos($data, '>') + 1);
        $temp = explode(':', substr($data, 0, strpos($data, '</span>')));
        $var[$i]["arrival"] = $temp[0] * 3600 + $temp[1] * 60 + $temp[2];
        $data = substr($data, strpos($data, RPOINT_TIMER_AT) + strlen(RPOINT_TIMER_AT));
        $data = substr($data, strpos($data, ' ') + 1);
        $temp = explode(':', substr($data, 0, strpos($data, '</span>')));
        $var[$i]["at"] = $temp[0] * 3600 + $temp[1] * 60 + $temp[2];
      }
      if (strpos($data, RPOINT_CROP_USAGE) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_CROP_USAGE) + strlen(RPOINT_CROP_USAGE));
        $var[$i]["consumes"] = substr($data, 0, strpos($data, '<'));
      }
      if (strpos($data, RPOINT_CROP_USAGE2) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_CROP_USAGE2) + strlen(RPOINT_CROP_USAGE2));
        $var[$i]["consumes"] = substr($data, 0, strpos($data, '<'));
      }
      if (strpos($data, RPOINT_CROP_USAGE3) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_CROP_USAGE3) + strlen(RPOINT_CROP_USAGE3));
        $var[$i]["consumes"] = substr($data, 0, strpos($data, '<'));
      }
      if (strpos($data, RPOINT_CALLBACK_URL) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_CALLBACK_URL) + strlen(RPOINT_CALLBACK_URL));
        $var[$i]["callbackurl"] = substr($data, 0, strpos($data, '">'));
        if (empty($var[$i]["arrival"])) $var[$i]["callbackurl"] = 'd='.$var[$i]["callbackurl"];
      }
      if (strpos($data, RPOINT_CANCEL) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_CANCEL) + strlen(RPOINT_CANCEL));
        $var[$i]["callbackurl"] = str_replacE('&amp;', '&', substr($data, 0, strpos($data, '">')));
      }
      if (strpos($data, RPOINT_SHOOT_HOSTAGE) !== False)
      {
        $data = substr($data, strpos($data, RPOINT_SHOOT_HOSTAGE) + strlen(RPOINT_SHOOT_HOSTAGE));
        $var[$i]["shoot_hostage"] = "id=".substr($data, 0, strpos($data, '">'));
      }
      if (!strpos($text, RPOINT_BOX_START)) break;
    }
    //print_r($var);
    return $var;
  }
  
  function travian_return_srtc($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "a2b.php");
    if (strpos($text, RPOINT_DEFINE_ROMA) !== False) $var["type"] = TROOPS_ROMANS;
    else if (strpos($text, RPOINT_DEFINE_GAUL) !== False) $var["type"] = TROOPS_GAULS;
    else $var["type"] = TROOPS_TEUTONS;
    for ($i = 1; $i <= 11; $i++)
    {
      $str = SENDT_TROOPS_START.$i.SENDT_TROOPS_END;
      if (strpos($text, $str) !== False)
      {
        $temp = Substr($text, strpos($text, $str) + strlen($str));
        $temp = substr($temp, 0, strpos($temp, ';'));
        $var[$i] = $temp;
      }
      else $var[$i] = 0;
    }
    return $var;
  }

  function travian_return_report_info($handle, $id, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "berichte.php?id=$id");
    $text = substr($text, strpos($text, S7_CLASS_TD) + strlen(S7_CLASS_TD));
    $text = substr($text, strpos($text, S7_CLASS_TD) + strlen(S7_CLASS_TD));
    $var["title"] = substr($text, 0, strpos($text, '</td>'));
    $text = substr($text, strpos($text, S7_CLASS_TD) + strlen(S7_CLASS_TD));
    $temp = substr($text, 0, strpos($text, '</span>'));
    $temp1 = substr($temp, strpos($temp, ' ') + 1);
    $temp2 = substr($temp1, 0, strpos($temp1, ' '));
    $var["date"][0] = $temp2;
    $temp1 = substr($temp1, strpos($temp1, ' ') + 1);
    $var["date"][1] = substr($temp1, strpos($temp1, ' ') + 1);
    $x = explode(' ', $var["date"][0]." ".$var["date"][1]);
    $x[0] = explode('.', $x[0]);
    $x[0][2] = "20".$x[0][2];
    $x[1] = explode(':', $x[1]);
    $var["date"] = mktime($x[1][0], $x[1][1], 0, $x[0][1], $x[0][0], $x[0][2]);
    if (!strpos($text, REPORT_TROOP_EXISTS))
    {
      $var["trade"] = True;
      $text = substr($text, strpos($text, REPORT_PLAYER_NAME) + strlen(REPORT_PLAYER_NAME));
      $var["player"]["id"] = substr($text, 0, strpos($text, '"'));
      $temp = substr($text, strpos($text, '>') + 1);
      $var["player"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
      $text = substr($text, strpos($text, REPORT_VILLAGE_NAME) + strlen(REPORT_VILLAGE_NAME));
      $var["village"]["url"] = substr($text, 0, strpos($text, '"'));
      $temp = substr($text, strpos($text, '>') + 1);
      $var["village"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
      $text = substr($text, strpos($text, REPORT_TRADE_RESS) + strlen(REPORT_TRADE_RESS));
      $text = substr($text, strpos($text, '>') + 1);
      $text = substr($text, 0, strpos($text, '</td>'));
      $var["resource"] = explode('|', str_replace(' ', '', strip_tags($text)));
    }
    else
    {
      $var["trade"] = False;
      if (strpos($text, REPORT_ATTACKER) !== False)
      {
        $text = substr($text, strpos($text, REPORT_PLAYER_NAME) + strlen(REPORT_PLAYER_NAME));
        $var[0]["player"]["id"] = substr($text, 0, strpos($text, '"'));
        $temp = substr($text, strpos($text, '>') + 1);
        $var[0]["player"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
        $text = substr($text, strpos($text, REPORT_VILLAGE_NAME) + strlen(REPORT_VILLAGE_NAME));
        $var[0]["village"]["url"] = substr($text, 0, strpos($text, '"'));
        $temp = substr($text, strpos($text, '>') + 1);
        $var[0]["village"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
        $var["type"] = 1;
        $text = substr($text, 0, strpos($text, REPORT_END));
        $part1 = substr($text, 0, strpos($text, REPORT_SEPARATOR) + strlen(REPORT_SEPARATOR));
        if (strpos($part1, REPORT_DEFINE_TEUT) !== False) $var[0]["ttype"] = 3;
        elseif (strpos($part1, REPORT_DEFINE_ROMA) !== False) $var[0]["ttype"] = 1;
        elseif (strpos($part1, REPORT_DEFINE_GAUL) !== False) $var[0]["ttype"] = 2;
        elseif (strpos($part1, REPORT_DEFINE_NATUR) !== False) $var[0]["ttype"] = 4;
        else $var[0]["ttype"] = 5;
        $text = substr($text, strpos($text, REPORT_TROOP_START) + strlen(REPORT_TROOP_START));
        $text = substr($text, strpos($text, '</td>') + 5);
        $data = substr($text, 0, strpos($text, '</tr>'));
        $var[0]["troops"] = explode('|', substr(str_replace(array('<td', '</td>', 'class="c">', '>', ' '), array('|', '', '', '', ''), $data), 1));
        $text = substr($text, strpos($text, REPORT_TROOP_START) + strlen(REPORT_TROOP_START));
        $text = substr($text, strpos($text, '</td>') + 5);
        $data = substr($text, 0, strpos($text, '</tr>'));
        $var[0]["casualties"] = explode('|', substr(str_replace(array('<td', '</td>', 'class="c">', '>', ' '), array('|', '', '', '', ''), $data), 1));
        $temp = strpos($text, 0, strpos($text, REPORT_SEGMENT_END));
        if (strpos($temp, REPORT_TROOP_START) !== False)
        {
          $text = substr($text, strpos($text, REPORT_TROOP_START) + strlen(REPORT_TROOP_START));
          $text = substr($text, strpos($text, '</td>') + 5);
          $data = substr($text, 0, strpos($text, '</tr>'));
          $var[0]["prisoners"] = explode('|', substr(str_replace(array('<td', '</td>', 'class="c">', '>', ' '), array('|', '', '', '', ''), $data), 1));
        }
        else $var[0]["prisoners"] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        $data = substr($text, 0, strpos($text, REPORT_SEGMENT_END));
        $data = str_replace('class="s7" ', '', $data);
        $x = -1;
        while (True)
        {
          $x++;
          $data = substr($data, strpos($data, REPORT_ADD_INFO) + strlen(REPORT_ADD_INFO));
          if (strpos($data, REPORT_ADD_INFO) !== False) $temp = substr($data, 0, strpos($data, REPORT_ADD_INFO));
          else $temp = $data;
          $temp = "<x t=\"$temp";
          if (strpos($temp, 'img/un/u/') !== False) $temp = substr($temp, 0, strpos($temp, '</div>'));
          if (strpos($temp, 'img/un/r/1.gif') !== False) $resource = True;
          else $resource = False;
          $temp = strip_tags($temp);
          if (!$resource)
          {
            $temp = substr($temp, 1);
            if (strpos($temp, '.') !== False) $temp = substr($temp, 0, strpos($temp, '.'));
            $var[0]["info"][$x] = $temp;
          } //$var[0]["info"][$x] = substr($temp, 1);
          else $var[0]["taken"] = explode(' ', $temp);
          if (!strpos($data, REPORT_ADD_INFO)) break;
        }
        $i = 0;
        while (true)
        {
          $i++;
          $text = substr($text, strpos($text, REPORT_SEPARATOR) + strlen(REPORT_SEPARATOR));
          $tempy = $text;
          $tempy = substr($tempy, strpos($tempy, REPORT_PLAYER_NAME) + strlen(REPORT_PLAYER_NAME));
          $var[$i]["player"]["id"] = substr($tempy, 0, strpos($tempy, '"'));
          $temp = substr($tempy, strpos($tempy, '>') + 1);
          $var[$i]["player"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
          if (!empty($var[$i]["player"]["name"]))
          {
            $tempy = substr($tempy, strpos($tempy, REPORT_VILLAGE_NAME) + strlen(REPORT_VILLAGE_NAME));
            $var[$i]["village"]["url"] = substr($tempy, 0, strpos($tempy, '"'));
            $temp = substr($tempy, strpos($tempy, '>') + 1);
            $var[$i]["village"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
          }
          else
          {
            $var[$i]["player"]["id"] = -1;
            $var[$i]["player"]["name"] = $var[1]["player"]["name"];
            $var[$i]["village"]["name"] = $var[1]["village"]["name"];
          }
          //$text = substr($text, strpos($text, REPORT_SEPARATOR) + strlen(REPORT_SEPARATOR));
          $part1 = substr($text, strpos($text, REPORT_SEPARATOR) + strlen(REPORT_SEPARATOR));
          if (strpos($part1, REPORT_SEPARATOR) !== fALSE) $part1 = substr($part1, 0, strpos($part1, REPORT_SEPARATOR));
          if (strpos($part1, REPORT_DEFINE_TEUT) !== False) $var[$i]["ttype"] = 3;
          elseif (strpos($part1, REPORT_DEFINE_ROMA) !== False) $var[$i]["ttype"] = 1;
          elseif (strpos($part1, REPORT_DEFINE_GAUL) !== False) $var[$i]["ttype"] = 2;
          elseif (strpos($part1, REPORT_DEFINE_NATUR) !== False) $var[$i]["ttype"] = 4;
          else $var[$i]["ttype"] = 5;
          $text = substr($text, strpos($text, REPORT_TROOP_START) + strlen(REPORT_TROOP_START));
          $text = substr($text, strpos($text, '</td>') + 5);
          $data = substr($text, 0, strpos($text, '</tr>'));
          $var[$i]["troops"] = explode('|', substr(str_replace(array('<td', '</td>', 'class="c">', '>', ' '), array('|', '', '', '', ''), $data), 1));
          $temp = substr($text, 0, strpos($text, REPORT_SEGMENT_END));
          if (strpos($temp, REPORT_TROOP_START) !== False)
          {
            $temp = substr($temp, strpos($temp, REPORT_TROOP_START) + strlen(REPORT_TROOP_START));
            $data = substr($temp, strpos($temp, '</td>'));
            $var[$i]["casualties"] = explode('|', substr(str_replace(array('<td', '</td>', 'class="c">', '>', ' '), array('|', '', '', '', ''), $data), 1));
          }
          if (!strpos($text, REPORT_SEPARATOR)) break;
        }
      }
      else
      {
        $var["type"] = 2;
        $text = substr($text, strpos($text, REPORT_PLAYER_NAME) + strlen(REPORT_PLAYER_NAME));
        $var["player"]["id"] = substr($text, 0, strpos($text, '"'));
        $temp = substr($text, strpos($text, '>') + 1);
        $var["player"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
        $text = substr($text, strpos($text, REPORT_VILLAGE_NAME) + strlen(REPORT_VILLAGE_NAME));
        $var["village"]["url"] = substr($text, 0, strpos($text, '"'));
        $temp = substr($text, strpos($text, '>') + 1);
        $var["village"]["name"] = substr($temp, 0, strpos($temp, '</a>'));
        if (strpos($text, REPORT_DEFINE_TEUT) !== False) $var["ttype"] = 1;
        elseif (strpos($text, REPORT_DEFINE_ROMA) !== False) $var["ttype"] = 2;
        elseif (strpos($part1, REPORT_DEFINE_GAUL) !== False) $var["ttype"] = 2;
        elseif (strpos($part1, REPORT_DEFINE_NATUR) !== False) $var["ttype"] = 4;
        else $var["ttype"] = 4;
        $text = substr($text, strpos($text, REPORT_TROOP_START) + strlen(REPORT_TROOP_START));
        $text = substr($text, strpos($text, '</td>') + 5);
        $data = substr($text, 0, strpos($text, '</tr>'));
        $var["troops"] = explode('|', substr(str_replace(array('<td', '</td>', 'class="c">', '>', ' '), array('|', '', '', '', ''), $data), 1));
        $text = substr($text, strpos($text, REPORT_RESOURCE_S) + 1 + strlen(REPORT_RESOURCE_S));
        $var["cropuse"] = substr($text, 0, strpos($text, REPORT_CROP_USE));
      }
    }
    return $var;
  }
  
  function travian_send_troops($handle, $troops, $type, $cata = 99, $cata2 = 99, $spy = 2, $coordinates = NULL, $village = NULL, $retinfo = False)
  {
    _travian_restore_cookie($handle);
    $request = array(
      "b"     => "1",
      "t1"    => $troops[0],
      "t2"    => $troops[1],
      "t3"    => $troops[2],
      "t4"    => $troops[3],
      "t5"    => $troops[4],
      "t6"    => $troops[5],
      "t7"    => $troops[6],
      "t8"    => $troops[7],
      "t9"    => $troops[8],
      "t10"   => $troops[9],
      "t11"   => $troops[10],
      "c"     => $type,
      "dname" => $village,
      "x"     => $coordinates[0],
      "y"     => $coordinates[1]
    );
    $info = http_post("http://".$handle["serv"]."/a2b.php", $request);
    $text = $info["body"];
    if (!strpos($text, SENDT_ERROR))
    {
      $text = substr($text, strpos($text, SENDT_VILLAGE) + strlen(SENDT_VILLAGE));
      $var["village"]["id"] = "d=".substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, '>') + 1);
      $temp = substr($text, 0, strpos($text, '</a>'));
      $var["village"]["name"] = substr($temp, 0, strpos($temp, '(') - 1);
      $temp = substr($temp, strpos($temp, '(') + 1);
      $temp = substr($temp, 0, strlen($temp) - 1);
      $var["village"]["coord"] = explode("|", $temp);
      $text = substr($text, strpos($text, SENDT_PLAYER) + strlen(SENDT_PLAYER));
      $var["player"]["uid"] = substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, '>') + 1);
      $var["player"]["name"] = substr($text, 0, strpos($text, '</a>'));
      $text = substr($text, strpos($text, SENDT_TD_TIME) + strlen(SENDT_TD_TIME));
      $text = substr($text, strpos($text, ' ') + 1);
      $x = explode(':', substr($text, 0, strpos($text, ' ')));
      $var["time"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      $text = substr($text, strpos($text, '<span id=tp2>') + strlen('<span id=tp2>'));
      $x = explode(':', substr($text, 0, strpos($text, '<')));
      $var["at"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      for ($i = 1; $i <= 15; $i++)
      {
        $text = substr($text, strpos($text, SENDT_HIDDEN) + strlen(SENDT_HIDDEN));
        $name = substr($text, 0, strpos($text, '"'));
        $text = substr($text, strpos($text, SENDT_HIDDEN_V) + strlen(SENDT_HIDDEN_V));
        $var[$name] = substr($text, 0, strpos($text, '"'));
      }
      if (!$retinfo)
      {
        $request = array(
          "id"    => $var["id"],
          "a"     => $var["a"],
          "c"     => $var["c"],
          "kid"   => $var["kid"],
          "kata"  => $cata1,
          "kata2" => $cata2,
          "spy"   => $spy,
          "s1"    => "ok"
        );
        for ($i = 1; $i <= 11; $i++) $request["t$i"] = $var["t$i"];
        http_post("http://".$handle["serv"]."/a2b.php", $request);
        $var = null;
        $var = True;
      }
    }
    else
    {
      $text = substr($text, strpos($text, SENDT_ERROR) + strlen(SENDT_ERROR));
      $var["error"] = substr($text, 0, strpos($text, '<'));
    }
    //print_r($var);
    return $var;
  }

  function travian_return_troop_upgrade_info($handle, $id, $parse = False, $ids = "id")
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id");
    $i = -1;
    $t = substr($text, strpos($text, TROOP_UPGRATE_SEP) + strlen(TROOP_UPGRADE_SEP));
    $x = strpos($t, '</tr></table><br>');
    if (!$x) $x = strpos($t, '</tr></table><p');
    $t = substr($t, 0, $x);
    $temp = explode(TROOP_UPGRATE_SEP, $t);
    foreach ($temp as $value)
    {
      $i++;
      $value = substr($value, strpos($value, TROOP_UPGRADE_START) + strlen(TROOP_UPGRADE_START));
      $value = substr($value, strpos($value, '">') + 2);
      $var["train"][$i]["name"] = substr($value, 0, strpos($value, '</a>'));
      if (strpos($value, TROOP_UPGRADE_LEVEL) !== False)
      {
        $value = substr($value, strpos($value, TROOP_UPGRADE_LEVEL) + strleN(TROOP_UPGRADE_LEVEL));
        $temp = substr($value, strpos($value, ' ') + 1);
        $var["train"][$i]["level"] = substr($temp, 0, strpos($temp, ')'));
      }
      $value = substr($value, strpos($value, TROOP_UPGRADE_RES) + strlen(TROOP_UPGRADE_RES));
      $temp = substr($value, 0, strpos($value, '</td></tr></table>'));
      $var["train"][$i]["requires"] = explode('|', strip_tags(str_replace(' ', '', $temp)));
      $x = explode(':', $var["train"][$i]["requires"][4]);
      $var["train"][$i]["requires"][4] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      if (!strpos($value, TROOP_UPGRADE_LINK))
      {
        $temp = substr($value, strpos($value, TROOP_UPGRADE_ERROR) + strlen(TROOP_UPGRADE_ERROR));
        $temp = substr($temp, 0, strpos($temp, '</div></td>'));
        $var["train"][$i]["error"] = str_replace('<br>', ' ', $temp);
      }
      else
      {
        $temp = substr($value, strpos($value, TROOP_UPGRADE_LINK) + strlen(TROOP_UPGRADE_LINK));
        $var["train"][$i]["link"] = "id=".substr($temp, 0, strpos($temp, '">'));
      }
    }
    if (strpos($text, TROOP_UPGRADE_NAME) !== False)
    {
      $text = substr($text, strpos($text, TROOP_UPGRADE_NAME) + strleN(TROOP_UPGRADE_NAME));
      $var["atrain"]["name"] = substr($text, 0, strpos($text, '</td>'));
      $text = substr($text, strpos($text, TROOP_UPGRADE_TIME) + strleN(TROOP_UPGRADE_TIME));
      $x = explode(':', substr($text, 0, strpos($text, '</span></td>')));
      $var["atrain"]["remain"] = $x[0] * 3600 + $x[1] * 60;
      $text = substr($text, strpos($text, TROOP_UPGRADE_AT) + strleN(TROOP_UPGRADE_AT));
      $x = explode(':', substr($text, 0, strpos($text, '</span><span>')));
      $var["atrain"]["at"] = $x[0] * 3600 + $x[1] * 60;
    }
    return $var;
  }

  function travian_return_main_building_info($handle, $id = 26, $parse = False, $ids = "id")
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id");
    if (strpos($text, MAIN_REM_ACTIVE) !== False)
    {
      $var["active_dem"] = True;
      $text = substr($text, strpos($text, MAIN_REM_LINK) + strleN(MAIN_REM_LINK));
      $var["url"] = "gid=".substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, '"></a></td><td>') + strleN('"></a></td><td>'));
      $var["title"] = substr($text, 0, strpos($text, MAIN_TITLE_TIMER));
      $text = substr($text, strpos($text, MAIN_TITLE_TIMER) + strleN(MAIN_TITLE_TIMER));
      $x = explode(':', substr($text, 0, strpos($text, '</span>')));
      $var["remain"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      $var["at"] = substr($text, strpos($text, CONST_SPAN) - CONST_SPANT);
      $var["at"] = explode(':', substr($var["at"], 0, strpos($var["at"], CONST_SPAN)));
      $var["at"] = $var["at"][0] * 3600 + $var["at"][1] * 60;
      $temp = substr($var["title"], strlen($var["title"]) - 3);
      $temp = substr($temp, 0, strlen($temp) - 1);
      if ($temp[0] == ' ') $temp = substr($temp, 1);
      $var["level"] = $temp;
    }
    else
    {
      $var["active_dem"] = False;
      $text = substr($text, strpos($text, MAIN_GID_HIDDEN) + strleN(MAIN_GID_HIDDEN));
      $var["gid"] = substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, '" value="') + strlen('" value="'));
      $var["a"] = substr($text, 0, strpos($text, '"'));
      $i = 0;
      while (True)
      {
        $i++;
        $text = substr($text, strpos($text, MAIN_OPTION) + strlen(MAIN_OPTION));
        $var[$i]["id"] = substr($text, 0, strpos($text, '"'));
        $text = substr($text, strpos($text, '">') + 2);
        $var[$i]["name"] = substr($text, 0, strpos($text, '</option>'));
        $var[$i]["ok"] = True;
        if (strpos($var[$i]["name"], '<leer>') !== False) $var[$i]["ok"] = False;
        if (!strpos($text, MAIN_OPTION)) break;
      }
    }
    return $var;
  }

  function travian_return_reports($handle, $parse = False, $num = 0, $page = 0)
  {
    if ($num == 0) $num = "";
    $page = $page * 10;
    $text = _travian_parse_needed($handle, $parse, "berichte.php?s=$page&t=$num");
    if (strpos($text, REPORT_NONE) != False)
    {
      return False;
      exit;
    }
    $i = -1;
    while (True)
    {
      $text = substr($text, strpos($text, REPORT_START) + strlen(REPORT_START));
      $i++;
      $text = substr($text, strpos($text, 'value="') + strlen('value="'));
      $var[$i]["ivalue"] = substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, REPORT_URL_LONG.$var[$i]["ivalue"].'">') + strleN(REPORT_URL_LONG.$var[$i]["ivalue"].'">'));
      $var[$i]["title"] = substr($text, 0, strpos($text, '</a>'));
      $temp = substr($text, 0, strpos($text, '</td>'));
      $text = substr($text, strpos($text, REPORT_DATE) + strlen(REPORT_DATE));
      $var[$i]["date"] = substr($text, 0, strpos($text, '</td>'));
      $x = explode(' ', substr($text, 0, strpos($text, '</td>')));
      $x[0] = explode('.', $x[0]);
      $x[1] = explode(':', $x[1]);
      $var[$i]["date"] = mktime($x[1][0], $x[1][1], 0, $x[0][1], $x[0][0], $x[0][2]);
      $temp = substr($temp, strpos($temp, '</a> ') + 5);
      if (strpos($temp, '(') !== False) $var[$i]["new"] = True;
      else $var[$i]["new"] = False;
      if (!strpos($text, REPORT_START)) break;
    }
    return($var);
  }

  function travian_return_villages($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    if (strpos($text, "right dlist1") == False)
    {
      $villages["count"] = 1;
      $villages["single"] = True;
    }
    else
    {
      $loop = True; $i = 0;
      while ($loop)
      {
        $i++;
        $text = substr($text, strpos($text, "newdid") + 7);
        if (strpos(substr($text, 0, strpos($text, '</tr>')), '"active_vl"') != False) $villages[$i]["active"] = True;
        else $villages[$i]["active"] = False;
        $villages[$i]["id"] = substr($text, 0, strpos($text, '"'));
        $villages[$i]["name"] = substr($text, strpos($text, '">') + 2, strpos($text, '</a>'));
        $villages[$i]["name"] = substr($villages[$i]["name"], 0, strpos($villages[$i]["name"], '<'));
        $villages[$i]["x"] = substr($text, strpos($text, ">(") + 2);
        $villages[$i]["x"] = substr($villages[$i]["x"], 0, strpos($villages[$i]["x"], "<"));
        $villages[$i]["y"] = substr($text, strpos($text, '3">') + 3);
        $villages[$i]["y"] = substr($villages[$i]["y"], 0, strpos($villages[$i]["y"], "<") - 1);
        $loop = strpos($text, "newdid");
      }
    }
    $villages["count"] = $i;
    return $villages;
  }

  // This function REQUIRES handle. There is no parse, as you can see ^^
  function travian_send_resources($handle, $id, $resources, $coordinates = NULL, $name = NULL, $retinfo = False, $gid = False, $html = NULL)
  {
    _travian_restore_cookie($handle);
    if ($gid)
    {
      if (empty($html)) $html = http_get("http://".$handle["serv"]."/build.php?gid=".BUILDING_MARKET);
      $id = substr($html["body"], strpos($html["body"], SENDT_ID) + strlen(SENDT_ID));
      $id = substr($id, 0, strpos($id, '"'));
    }
    $request = Array(
      "r1"     => $resources[0],
      "r2"     => $resources[1],
      "r3"     => $resources[2],
      "r4"     => $resources[3],
      "dname"  => $name,
      "x"      => $coordinates[0],
      "y"      => $coordinates[1],
      "id"     => $id,
      "s1"     => "ok"
    );
    $info = http_post("http://".$handle["serv"]."/build.php", $request);
    $text = $info["body"];
    if (!strpos($text, TRADE_SEND_ERROR))
    {
      $text = substr($text, strpos($text, TRADE_INPUT_HIDDEN) + strlen(TRADE_INPUT_HIDDEN));
      $var["a"] = substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, TRADE_INPUT_HIDDEN2) + strlen(TRADE_INPUT_HIDDEN2));
      $var["sz"] = substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, TRADE_INPUT_HIDDEN3) + strlen(TRADE_INPUT_HIDDEN3));
      $var["kid"] = substr($text, 0, strpos($text, '"'));
      $var["resources"] = $resources;
      $text = substr($text, strpos($text, TRADE_VILLAGE_NAME) + strlen(TRADE_VILLAGE_NAME));
      $var["vname"] = substr($text, 0, strpos($text, ' ('));
      if (!empty($coordinates)) $var["coord"] = $coordinates;
      else
      {
        $temp = substr($text, strpos($text, ' (') + 2);
        $temp = substr($temp, 0, strpos($temp, ')'));
        $var["coord"] = explode('|', $temp);
      }
      $text = substr($text, strpos($text, TRADE_PLAYER_NAME) + strlen(TRADE_PLAYER_NAME));
      $var["player"]["uid"] = substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, '">') + 2);
      $var["player"]["name"] = substr($text, 0, strpos($text, '</a>'));
      $text = substr($text, strpos($text, TRADE_TTIME) + strlen(TRADE_TTIME));
      $text = substr($text, strpos($text, '</td><td>') + 9);
      $x = explode(':', substr($text, 0, strpos($text, '</td></tr>')));
      $var["time"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      $text = substr($text, strpos($text, TRADE_TTIME) + strlen(TRADE_TTIME));
      $text = substr($text, strpos($text, '</td><td>') + 9);
      $var["merchants"] = substr($text, 0, strpos($text, '</td></tr>'));
      $var["id"] = $id;
    }
    else
    {
      $var = substr($text, strpos($text, TRADE_SEND_ERROR) + strleN(TRADE_SEND_ERROR));
      $var = substr($var, 0, strpos($var, '</p>'));
      return array("error" => $var);
    }
    if (!$retinfo)
    {
      $request = NULL;
      $request = Array(
        "r1"     => $resources[0],
        "r2"     => $resources[1],
        "r3"     => $resources[2],
        "r4"     => $resources[3],
        "a"      => $var["a"],
        "sz"     => $var["sz"],
        "kid"    => $var["kid"],
        "id"     => $id,
        "s1"     => "ok"
      );
      http_post("http://".$handle["serv"]."/build.php", $request);
      $var = NULL;
      $var = True;
    }
    return $var;
  }
  
  function travian_return_transports($handle, $id, $parse = False, $ids = "id")
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id");
    $temp = substr($text, strpos($text, BUILD_NAME_START) + strlen(BUILD_NAME_START));
    $temp = substr($text, 0, strpos($text, '</b></h1>'));
    $result["merchants"][0] = substr($temp, strlen($temp) - 2);
    $temp = substr($text, strpos($text, TRADE_MERCHANTS) + strlen(TRADE_MERCHANTS));
    $result["merchants"][1] = substr($temp, 0, strpos($temp, ';'));
    $temp = substr($text, strpos($text, TRADE_CARRY) + strlen(TRADE_CARRY));
    $result["merchants"][2] = substr($temp, 0, strpos($temp, ';'));
    //$text = substr($text, strpos($text, TRADE_TRANSPORT) + strlen(TRADE_TRANSPORT));
    $i = -1;
    if (strpos($text, TRADE_TRANSPORT) != False)
    {
      while (True)
      {
        $text = substr($text, strpos($text, TRADE_TRANSPORT) + strlen(TRADE_TRANSPORT));
        $i++;
        $temp = substr($text, 0, strpos($text, TRADE_TRANSPORT_END));
        $temp = substr($temp, strpos($temp, TRADE_PLAYER) + strlen(TRADE_PLAYER));
        $result["transport"][$i]["player"]["uid"] = substr($temp, 0, strpos($temp, '"'));
        $temp = substr($temp, strpos($temp, TRADE_LOCATION) + strlen(TRADE_LOCATION));
        $result["transport"][$i]["player"]["name"] = substr($temp, 0, strpos($temp, '<'));
        $temp = substr($temp, strpos($temp, TRADE_VILLAGESEND) + strlen(TRADE_VILLAGESEND));
        $result["transport"][$i]["village"]["link"] = "d=".substr($temp, 0, strpos($temp, '"'));
        $temp = substr($temp, strpos($temp, TRADE_LOCATION) + strlen(TRADE_LOCATION));
        $result["transport"][$i]["village"]["name"] = substr($temp, 0, strpos($temp, '<'));
        $temp = substr($temp, strpos($temp, TRADE_TIMER) + strlen(TRADE_TIMER));
        $temp = substr($temp, strpos($temp, '>') + 1);
        $x = explode(':', substr($temp, 0, strpos($temp, '</span>')));
        $result["transport"][$i]["time"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
        $temp = substr($temp, strpos($temp, '</td><td>') + strlen('</td><td>'));
        $te = substr($temp, 0, strpos($temp, '</td></tr>'));
        $x = explode(':', substr($te, strlen($te) - 5));
        $result["transport"][$i]["at"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
        $temp = substr($temp, strpos($temp, TRADE_RESOURCES) + strlen(TRADE_RESOURCES));
        $te = substr($temp, 0, strpos($temp, '"'));
        if ($te != TRADE_RETURN) $result["transport"][$i]["return"] = False;
        else $result["transport"][$i]["return"] = True;
        $temp = substr($temp, strpos($temp, '>') + 1);
        $temp = str_replace(" ", "", strip_tags($temp));
        $result["transport"][$i]["resources"] = explode("|", $temp);
        if (!strpos($text, TRADE_TRANSPORT)) break;
      }
    }
    return $result;
  }
  
  function travian_return_trade_info($handle, $id, $page = 0, $parse = False, $ids = "id")
  {
    $start = $page * 40;
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id&t=1&u=$start");
    if (strpos($text, TRADE_FINISH_LIST) !== false) $result["end_list"] = True;
    else $result["end_list"] = False;
    $i = -1;
    $text = substr($text, strpos($text, TRADE_OFFER_RES));
    $temp = explode("</td></tr>", $text);
    foreach ($temp as $key => $value)
    {
      $i++;
      //if ($i == 15) break;
      $te = substr($value, strpos($value, TRADE_OFFER_RES) + strlen(TRADE_OFFER_RES));
      $result[$i]["offer"]["type"] = _travian_return_resource_id(substr($te, 0, strpos($te, '"')));
      if ($result[$i]["offer"]["type"] == 0)
      {
         $i--;
         break;
      }
      $te1 = substr($te, strpos($te, '<td>') + strlen('<td>'));
      $result[$i]["offer"]["count"] = substr($te1, 0, strpos($te1, '<'));
      $te = substr($te, strpos($te, TRADE_OFFER_RES) + strlen(TRADE_OFFER_RES));
      $result[$i]["demand"]["type"] = _travian_return_resource_id(substr($te, 0, strpos($te, '"')));
      $te1 = substr($te, strpos($te, '<td>') + strlen('<td>'));
      $result[$i]["demand"]["count"] = substr($te1, 0, strpos($te1, '<'));
      $te = substr($te, strpos($te, '<td title="') + strlen('<td title="'));
      $result[$i]["owner"]["alliance"] = substr($te, 0, strpos($te, '"'));
      $te = substr($te, strpos($te, TRADE_OFFER_PLAYER) + strlen(TRADE_OFFER_PLAYER));
      $result[$i]["owner"]["ownerurl"] = "d=".substr($te, 0, strpos($te, '">'));
      $te = substr($te, strpos($te, '">') + 2);
      $result[$i]["owner"]["name"] = substr($te, 0, strpos($te, '</a></td>'));
      $te = substr($te, strpos($te, '<td>') + 4);
      $x = explode(':', substr($te, 0, strpos($te, '</td>')));
      $result[$i]["time"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      if (strpos($value, '<td class="c">')) $result[$i]["cando"] = False;
      else
      {
        $result[$i]["cando"] = True;
        $te = substr($te, strpos($te, '</td><td><a href="build.php') + strlen('</td><td><a href="build.php'));
        $result[$i]["link"] = substr($te, 1, strpos($te, '"') - 1);
      }
    }
    //$result["count"] = $i;
    return $result;
  }
  
  function travian_return_bid_info($handle, $id, $parse = False, $ids = "id")
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id&t=2");
    $temp = substr($text, strpos($text, TRADE_MERCHANT_BID) + strlen(TRADE_MERCHANT_BID));
    $temp = substr($temp, strpos($temp, ' ') + 1);
    $temp = substr($temp, 0, strpos($temp, '<'));
    $result["mer"] = explode('/', $temp);
    if (!strpos($text, TRADE_BID_START)) $result["bids"]["count"] = -1;
    else
    {
      $i = -1;
      while (True)
      {
        $i++;
        $text = substr($text, strpos($text, TRADE_BID_START) + strleN(TRADE_BID_START));
        $tempy = substr($text, 0, strpos($text, '"'));
        $result["bids"][$i]["url"] = substr($tempy, 10);
        $text = substr($text, strpos($text, '<td><img src="') + strlen('<td><img src="'));
        $temp = substr($text, 0, strpos($text, '"'));
        $result["bids"][$i]["offer"]["type"] = _travian_return_resource_id($temp);
        $text = substr($text, strpos($text, ">") + 1);
        $result["bids"][$i]["offer"]["count"] = substr($text, 0, strpos($text, '<'));
        $text = substr($text, strpos($text, '<td><img src="') + strlen('<td><img src="'));
        $temp = substr($text, 0, strpos($text, '"'));
        $result["bids"][$i]["demand"]["type"] = _travian_return_resource_id($temp);
        $text = substr($text, strpos($text, ">") + 1);
        $result["bids"][$i]["demand"]["count"] = substr($text, 0, strpos($text, '<'));
        if (!strpos($text, TRADE_BID_START)) break;
      }
    }
    return $result;
  }

  function travian_return_train_info($handle, $id, $parse = False, $ids = "id")
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id");
    $text = substr($text, strpos($text, TRAIN_Z_START) + strlen(TRAIN_Z_START));
    $var["form"]["id"] = $id;
    $var["form"]["z"] = substr($text, 0, strpos($text, '"'));
    $var["form"]["a"] = substr($text, strpos($text, 'value="') + 7);
    $var["form"]["a"] = substr($var["form"]["a"], 0, strpos($var["form"]["a"], '"'));
    $i = -1;
    while (True)
    {
      $text = substr($text, strpos($text, TRAIN_TROOP_TYPE_S) + strlen(TRAIN_TROOP_TYPE_S));
      $i++;
      $var["form"][$i]["type"] = substr($text, 0, strpos($text, '.gif'));
      $text = substr($text, strpos($text, TRAIN_TNAME_START) + strlen(TRAIN_TNAME_START));
      $text = substr($text, strpos($text, '"> ') + 3);
      $var["form"][$i]["name"] = substr($text, 0, strpos($text, '</a>'));
      $text = substr($text, strpos($text, TRAIN_TROOPS_COUNT) + strlen(TRAIN_TROOPS_COUNT));
      $text = substr($text, strpos($text, ': ') + 2);
      $var["form"][$i]["exist"] = substr($text, 0, strpos($text, ')'));
      $text = substr($text, strpos($text, TRAIN_TROOPS_RES) + strlen(TRAIN_TROOPS_RES));
      $temp = substr($text, 0, strpos($text, '</td></tr></table>'));
      $temp = explode('|', str_replace(' ', '', strip_tags($temp)));
      $x = explode(':', $temp[5]);
      $temp[5] = $x[0] * 3600 + $x[1] * 60 + $x[2];
      $var["form"][$i]["req"] = $temp;
      $text = substr($text, strpos($text, TRAIN_TROOPS_CTRAIN) + strlen(TRAIN_TROOPS_CTRAIN));
      $var["form"][$i]["max"] = substr($text, 0, strpos($text, ')'));
      // Kept here only for backward compatibilty.
      $var["form"][$i]["fname"] = "t".($i + 1);
      if (strpos($text, TRAIN_TROOP_TYPE_S) === False) break;
    }
    if (strpos($text, TRAIN_IS_STH) !== False)
    {
      $i = -1;
      while (True)
      {
        $text = substr($text, strpos($text, TRAIN_IS_STH) + strlen(TRAIN_IS_STH));
        $i++;
        $var["train"][$i]["type"] = substr($text, 0, strpos($text, '.gif'));
        $text = substr($text, strpos($text, TRAIN_COUNT) + strlen(TRAIN_COUNT));
        $var["train"][$i]["count"] = substr($text, 0, strpos($text, '&'));
        $text = substr($text, strpos($text, TRAIN_NAME) + strlen(TRAIN_NAME));
        $var["train"][$i]["name"] = substr($text, 0, strpos($text, '</td>'));
        $text = substr($text, strpos($text, TRAIN_TIME) + strlen(TRAIN_TIME));
        $temp = substr($text, strpos($text, '>') + 1);
        $x = explode(':', substr($temp, 0, strpos($temp, '</span>')));
        $var["train"][$i]["time"] = $x[0] * 3600 + $x[1] * 60 + $x[2];
        if (strpos($text, TRAIN_IS_STH) === False) Break;
      }
      $temp = substr($text, strpos($text, TRAIN_NEXT_FIN) + strlen(TRAIN_NEXT_FIN));
      $temp = substr($temp, strpos($temp, '>') + 1);
      $temp = explode(':', substr($temp, 0, strpos($temp, '</span>')));
      $var["train"]["next"] = $temp[0] * 3600 + $temp[1] * 60 + $temp[2];
    }
    return $var;
  }

  function travian_active_construction($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    if (!strpos($text, CONST_START)) return False;
    for ($i = 0; $i <= 2; $i++)
    {
      $text = substr($text, strpos($text, CONST_START) + strlen(CONST_START));
      $var[$i]["url"] = "d=".substr($text, 0, strpos($text, '"'));
      $text = substr($text, strpos($text, CONST_X_END) + strlen(CONST_X_END));
      $var[$i]["title"] = substr($text, 0, strpos($text, CONST_TIMER));
      if (empty($var[$i]["title"]))
      {
        $var[$i]["url"] = NULL;
        break;
      }
      $text = substr($text, strpos($text, CONST_TIMER) + strlen(CONST_TIMER));
      $temp = explode(':', substr($text, strpos($text, '>') + 1, strpos($text, "<") - 2));
      $var[$i]["end"] = $temp[0] * 3600 + $temp[1] * 60 + $temp[2];
      $temp = explode(':', substr($text, strpos($text, CONST_SPAN) - CONST_SPANT, CONST_SPANT));
      $var[$i]["endat"] = $temp[0] * 3600 + $temp[1] * 60;
      $temp = substr($var[$i]["title"], strpos($var[$i]["title"], '('));
      $temp = substr($temp, strpos($temp, ' ') + 1);
      $var[$i]["level"] = substr($temp, 0, strlen($temp) - 1);
    }
    return $var;
  }

  function travian_return_construct_info($handle, $id, $parse = False, $ids = "id")
  {
    $text = _travian_parse_needed($handle, $parse, "build.php?$ids=$id");
    if ((strpos($text, BUILD_NAME_START2) == False) || (strpos($text, BUILD_NAME_FALSE) == True))
    {
      $text = substr($text, strpos($text, BUILD_NAME_START) + strlen(BUILD_NAME_START));
      $var[0]["name"] = substr($text, 0, strpos($text, "</b>"));
      $temp = substr($text, strpos($text, RES_DEF_B_START2) + strlen(RES_DEF_B_START2));
      $temp = substr($temp, 0, strpos($temp, '</td></tr></table>'));
      $temp = strip_tags(str_replace(" ", "", $temp));
      if (strlen($temp) < 100)
      {
        $var[0]["required"] = explode("|", $temp);
        $temp = explode(':', $var[0]["required"][5]);
        $var[0]["required"][5] = $temp[0] * 3600 + $temp[1] * 60 + $temp[2];
      }
      else $var[0]["required"] = -1;
      $text = substr($text, strpos($text, RES_DEF_B_START2) + strlen(RES_DEF_B_START2));
      if (((strpos($text, BUILD_START_ERROR) !== False) || (strpos($text, BUILD_START_ERROR2) !== False)) && (strpos($text, BUILD_A) === False))
      {
        $var[0]["error"] = substr($text, strpos($text, BUILD_START_ERROR) + strlen(BUILD_START_ERROR));
        $var[0]["error"] = substr($var[0]["error"], 0, strpos($var[0]["error"], '</span>'));
        if (strlen($var[0]["error"]) > 100)
        {
          $var[0]["error"] = substr($text, strpos($text, BUILD_START_ERROR2) + strlen(BUILD_START_ERROR2));
          $var[0]["error"] = substr($var[0]["error"], 0, strpos($var[0]["error"], '</p>'));
        }
        $var[0]["error"] = strip_tags($var[0]["error"]);
      }
      else
      {
        $var[0]["link"]["file"] = substr($text, strpos($text, BUILD_A) - 5);
        $var[0]["link"]["file"] = substr($var[0]["link"]["file"], 0, strpos($var[0]["link"]["file"], '?'));
        $var[0]["link"]["params"] = substr($text, strpos($text, BUILD_A) + 5);
        $var[0]["link"]["params"] = substr($var[0]["link"]["params"], 0, strpos($var[0]["link"]["params"], '"'));  //
      }
      //$var["size"] = 1;
    }
    else
    {
      $loop = True; $i = -1;
      while ($loop)
      {
        $i++;
        if (strpos($text, BUILD_NAME_START2) == False)
        {
          $loop = False;
          continue;
        }
        $text = substr($text, strpos($text, '<h2>') + 4);
        $var[$i]["name"] = substr($text, 0, strpos($text, "</h2>"));
        $temp = substr($text, strpos($text, RES_DEF_BUILD_START) + strlen(RES_DEF_BUILD_START));
        $temp = substr($temp, 0, strpos($temp, '<br />'));
        $temp = strip_tags(str_replace(array("  ", " ", "\n"), array("", "", ""), $temp));
        $var[$i]["required"] = explode("|", $temp);
        $temp = explode(':', $var[$i]["required"][5]);
        $var[$i]["required"][5] = $temp[0] * 3600 + $temp[1] * 60 + $temp[2];
        if ($var[$i]["required"][5] == 0)
        {
          unset($var[$i]);
          $loop = False;
          continue;
        }
        $te = substr($text, 0, strpos($text, '</table><br /><br />') + strlen('</table><br /><br />'));
        if ((strpos($te, BUILD_START_ERROR) != False) && (strpos($te, BUILD_A) === False))
        {
          $var[$i]["error"] = substr($text, strpos($text, BUILD_START_ERROR) + strlen(BUILD_START_ERROR));
          $var[$i]["error"] = substr($var[$i]["error"], 0, strpos($var[$i]["error"], '</span>'));
          $var[$i]["error"] = strip_tags($var[$i]["error"]);
        }
        else
        {
          $var[$i]["link"]["file"] = substr($text, strpos($text, BUILD_A) - 9);
          $var[$i]["link"]["file"] = substr($var[$i]["link"]["file"], 4, strpos($var[$i]["link"]["file"], '?') - 4);
          $var[$i]["link"]["params"] = substr($text, strpos($text, BUILD_A) + 1);
          $var[$i]["link"]["params"] = substr($var[$i]["link"]["params"], 4, strpos($var[$i]["link"]["params"], '"') - 4);
        }
      }
      //$var["size"] = $i - 1;
      //$var["multi"] = True;
    }
    #print_r($var);
    return $var;
  }

  function travian_logged_in($txt)
  {
    return strpos($txt, "<a href=\"karte.php\">");
  }

  function travian_extract_village_name($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    $startpos = strpos($text, VILLAGE_NAME) + strlen(VILLAGE_NAME);
    $village = substr($text, $startpos);
    return substr($village, 0, strpos($village, VILLAGE_NAME_END));
  }

  function travian_extract_villages($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    if (strpos($text, "right dlist1") == False)
    {
      //$villages["count"] = 0;
      $villages["single"] = True;
    }
    else
    {
      $loop = True; $i = -1;
      while ($loop)
      {
        $i++;
        $text = substr($text, strpos($text, "newdid") + 7);
        if (strpos(substr($text, 0, strpos($text, '</tr>')), '"active_vl"') != False) $villages[$i]["active"] = True;
        else $villages[$i]["active"] = False;
        $villages[$i]["id"] = substr($text, 0, strpos($text, '"'));
        $villages[$i]["name"] = substr($text, strpos($text, '">') + 2, strpos($text, '</a>'));
        $villages[$i]["name"] = substr($villages[$i]["name"], 0, strpos($villages[$i]["name"], '<'));
        $villages[$i]["x"] = substr($text, strpos($text, ">(") + 2);
        $villages[$i]["x"] = substr($villages[$i]["x"], 0, strpos($villages[$i]["x"], "<"));
        $villages[$i]["y"] = substr($text, strpos($text, '3">') + 3);
        $villages[$i]["y"] = substr($villages[$i]["y"], 0, strpos($villages[$i]["y"], "<") - 1);
        $loop = strpos($text, "newdid");
      }
    }
    //$villages["count"] = $i;
    return $villages;
  }
  
  function travian_extract_production($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    $temp[0] = substr($text, strpos($text, WOOD_START) + strlen(WOOD_START));
    $temp[1] = substr($temp[0], strpos($temp[0], CLAY_START) + strlen(CLAY_START));
    $temp[2] = substr($temp[1], strpos($temp[1], IRON_START) + strlen(IRON_START));
    $temp[3] = substr($temp[2], strpos($temp[2], CROP_START) + strlen(CROP_START));
    for($i = 0; $i <= 3; $i++) $temp[$i] = substr($temp[$i], 0, strpos($temp[$i], '>'));
    return $temp;
  }

  function travian_extract_resource_count($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    $temp[0] = substr($text, strpos($text, WOOD_START) + strlen(WOOD_START));
    $temp[1] = substr($temp[0], strpos($temp[0], CLAY_START) + strlen(CLAY_START));
    $temp[2] = substr($temp[1], strpos($temp[1], IRON_START) + strlen(IRON_START));
    $temp[3] = substr($temp[2], strpos($temp[2], CROP_START) + strlen(CROP_START));
    if (strpos($text, CROP_PROD_START) !== False) $temp[4] = substr($text, strpos($text, CROP_PROD_START) + strlen(CROP_PROD_START));
    else $temp[4] = substr($text, strpos($text, CROP_PROD_START2) + strlen(CROP_PROD_START2));
    for($i = 0; $i <= 4; $i++)
    {
      $temp[$i] = substr($temp[$i], strpos($temp[$i], '>') + 1);
      $temp[$i] = substr($temp[$i], 0, strpos($temp[$i], '<'));
    }
    if (substr($temp[4], 0, 6) == '&nbsp;') $temp[4] = substr($temp[4], 6);
    for ($i = 0; $i <= 4; $i++) $resources[$i] = explode("/", $temp[$i]);
    return $resources;
  }
  
  function travian_extract_resource_page_info($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf1.php");
    $resources = travian_extract_resource_count($text, True);
    $temp[0] = substr($text, strpos($text, RES_PROD_START) + strlen(RES_PROD_START) - 1);
    $temp[1] = substr($temp[0], strpos($temp[0], RES_PROD_START) + strlen(RES_PROD_START) - 1);
    $temp[2] = substr($temp[1], strpos($temp[1], RES_PROD_START) + strlen(RES_PROD_START) - 1);
    for ($i = 0; $i <= 2; $i++)
    {
      $temp[$i] = substr($temp[$i], 1, strpos($temp[$i], '&') - 1);
    }
    $temp[3] = $resources[4][1] - $resources[4][0];
    $production = $temp;
    
    $temp = substr($text, strpos($text, '<map name="rx">') + strlen('<map name="rx">'));
    $temp = substr($temp, 0, strpos($temp, '"></map>'));
    for ($i = 0; $i < 18; $i++)
    {
      $temp = substr($temp, strpos($temp, 'title="') + strlen('title="'));
      $omg = substr($temp, 0, strpos($temp, '"'));
      $fields[$i] = $omg;
    }
    // Now we extract field data.
    $crop_def = substr($fields[1], 0, strpos($fields[1], ' '));
    $wood_def = substr($fields[2], 0, strpos($fields[2], ' '));
    $clay_def = substr($fields[15], 0, strpos($fields[15], ' '));
    $iron_def = substr($fields[6], 0, strpos($fields[6], ' '));
    if ($iron_def == $crop_def) $iron_def = substr($fields[3], 0, strpos($fields[6], ' '));
    for ($i = 0; $i < 18; $i++)
    {
      $omg = $fields[$i];
      $fields[$i] = NULL;
      if (strpos($omg, $wood_def) !== False) $fields[$i][0] = 1;
      if (strpos($omg, $clay_def) !== False) $fields[$i][0] = 2;
      if (strpos($omg, $iron_def) !== False) $fields[$i][0] = 3;
      if (strpos($omg, $crop_def) !== False) $fields[$i][0] = 4;
      $fields[$i][1] = str_replace(' ', '', substr($omg, strlen($omg) - 2));
    }
    return array(
      "fields"       => $fields,
      "production"   => $production,
      "construction" => travian_active_construction($text, True)
    );
  }

  function travian_return_buildings_in_village($handle, $parse = False)
  {
    $text = _travian_parse_needed($handle, $parse, "dorf2.php");
    $otext = $text;
    $i = -1; $loop = True;
    while ($loop)
    {
      $i++;
      if(strpos($text, AREA) == False)
      {
        $loop = False;
        continue;
      }
      $text = substr($text, strpos($text, AREA) + strlen(AREA));
      $buildings[$i]["link"] = substr($text, 0, strpos($text, '"'));
      $buildings[$i]["link"] = substr($buildings[$i]["link"], 10);
      $buildings[$i]["name"] = substr($text, strpos($text, 'title="') + strlen('title="'));
      $buildings[$i]["name"] = substr($buildings[$i]["name"], 0, strpos($buildings[$i]["name"], '"'));
      if ($buildings[$i]["link"] == "id=40")
      {
        $i--;
        continue;
      }
    }
    // Added later on
    $text = $otext;
    if (strpos($text, 'img/un/g/g'.BUILDING_RALLYPOINT) !== False) $buildings[0]["gid"] = BUILDING_RALLYPOINT;
    else $buildings[0]["gid"] = BUILDING_FREE;
    $text = substr($text, strpos($text, '</div><img class="d1" src="') + strlen('</div><img class="d1" src="'));
    $buildings[$i]["gid"] = 0;
    for ($i = 0; $i <= 20; $i++)
    {
      if ($i > 0)
      {
        $text = substr($text, strpos($text, 'img/un/g/') + strlen('img/un/g/'));
        $temp = substr($text, 1, strpos($text, '.') - 1);
        if ($temp == "so") $temp = 0;
        $buildings[$i]["gid"] = $temp;
      }
      if ($buildings[$i]["gid"] != BUILDING_FREE)
      {
        $temp = substr($buildings[$i]["name"], strlen($buildings[$i]["name"]) - 2);
        if ($temp[0] == ' ') $temp = substr($temp, 1);
        $buildings[$i]["level"] = $temp;
      }
    }
    return array("buildings" => $buildings, "construction" => travian_active_construction($text, True));
  }
  
  function travian_logout($handle)
  {
    
    http_get("http://".$handle["serv"]."/logout.php");
    return True;
  }
  
  function travian_create($user, $pass, $server)
  {
    http_restore_cookies(NULL);
    $val = http_get("http://$server/");
    $tmp = substr($val["body"], strpos($val["body"], 'name="login" value="') + strlen('name="login" value="'));
    $logincode = substr($tmp, 0, strpos($tmp, '"'));
    $userid = substr($val["body"], strpos($val["body"], LOGIN_USER) + strlen(LOGIN_USER));
    $userid = substr($userid, 0, strpos($userid, '"'));
    $passid = substr($val["body"], strpos($val["body"], LOGIN_PASS) + strlen(LOGIN_PASS));
    $passid = substr($passid, 0, strpos($passid, '"'));
    $hidden = substr($val["body"], strpos($val["body"], LOGIN_HIDD) + strlen(LOGIN_HIDD));
    $hidden = substr($hidden, 0, strpos($hidden, '"'));
    $form = array("w"         => "",
                  "login"     => $logincode,
                  $userid     => $user,
                  $passid     => $pass,
                  $hidden     => "",
                  "autologin" => "ja");
    $response = http_post("http://$server/dorf1.php", $form);
    if (!travian_logged_in($response["body"])) return False;
    $ret = array("user"    => $user,
                 "pass"    => $pass,
                 "serv"    => $server,
                 "cookie"  => http_return_cookie());
    return $ret;
  }

